import { Injectable } from '@nestjs/common';
import { UsersService } from '@src/modules/users/users.service';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class AuthService {
   constructor(
      private readonly usersService: UsersService,
      private readonly jwtService: JwtService
   ) { }

   async validateUser(username: string, password: string): Promise<any> {
      const user = await this.usersService.findOne(username);
      if (user && user.password === password) {
         const { password, ...result } = user;
         return result;
      }
      return null;
   }

   async createToken(user: any) {
      const payload = { username: user.username };
      return {
         access_token: this.jwtService.sign(payload),
      };
   }
}
